// pages/game/blind.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    timu: [
      {
        desc: '你看到的字母是？',
        url: 'timu_1.png',
        width: '77.6',
        height: '36.8',
        answer: '1',
        option: [{
          desc: 'A和C',
          answer: '1',
        },{
            desc: 'A或C',
            answer: '2',
        },{
            desc: '看不到',
            answer: '3'
        }]
      },
      {
        desc: '你看到的数字是？',
        url: 'timu_2.png',
        width: '50.4',
        height: '50.93',
        answer: '1',
        option: [{
          desc: '数字6',
          answer: '1',
        }, {
            desc: '数字5',
          answer: '2',
        }, {
            desc: '看不到',
          answer: '3'
        }]
      },
      {
        desc: '你看到的数字是？',
        url: 'timu_3.png',
        width: '50.4',
        height: '50.93',
        answer: '3',
        option: [{
          desc: '数字6',
          answer: '1',
        }, {
          desc: '数字2',
          answer: '2',
        }, {
          desc: '数字26',
          answer: '3'
        }]
      },
      {
        desc: '你看到的数字是？',
        url: 'timu_4.png',
        width: '50.4',
        height: '50.93',
        answer: '2',
        option: [{
          desc: '数字6',
          answer: '1',
        }, {
          desc: '数字66',
          answer: '2',
        }, {
          desc: '看不到',
          answer: '3'
        }]
      },
      {
        desc: '你看到的数字是？',
        url: 'timu_5.png',
        width: '50.4',
        height: '50.93',
        answer: '1',
        option: [{
          desc: '数字5',
          answer: '1',
        }, {
          desc: '数字8',
          answer: '2',
        }, {
          desc: '看不到',
          answer: '3'
        }]
      },
      {
        desc: '你能看到什么？',
        url: 'timu_6.png',
        width: '50.4',
        height: '50.93',
        answer: '2',
        option: [{
          desc: '数字5',
          answer: '1',
        }, {
          desc: '无法辨别',
          answer: '2',
          }, {
            desc: '数字8',
            answer: '3',
          }]
      },
      {
        desc: '你看到的动物是？',
        url: 'timu_7.png',
        width: '50.4',
        height: '50.93',
        answer: '1',
        option: [{
          desc: '牛',
          answer: '1',
        }, {
          desc: '鹿',
          answer: '2',
        }]
      },
      {
        desc: '你看到的动物是？',
        url: 'timu_8.png',
        width: '50.4',
        height: '50.93',
        answer: '1',
        option: [{
          desc: '金鱼',
          answer: '1',
        }, {
          desc: '看不到',
          answer: '2',
        }]
      },
      {
        desc: '你看到的动物是？',
        url: 'timu_9.png',
        width: '50.4',
        height: '50.93',
        answer: '1',
        option: [{
          desc: '鸭和兔',
          answer: '1',
        }, {
          desc: '鸭',
          answer: '2',
        }]
      },
    ],
    curIndex: 0,
    curOption: 0,
    test_end: false,
    isClick: false,
    number: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let arr = this.data.timu
    for (let item of arr) {
      this.shuffle(item.option)
    }
    this.shuffle(arr)
    this.setData({
      timu: arr
    })
  },
  // 切换选项
  toggleOption(e){
    let that = this
    let audio = wx.createInnerAudioContext()
    audio.src = 'http://resource.160nurse.com/canvas/healthy/mp3/option.mp3'
    audio.autoplay = true
    audio.onPlay(() => {

    })
    audio.onEnded(() => {
      audio.destroy()
    })
    console.log(e)
    let answer = e.target.dataset.answer
    that.setData({
      curOption: answer
    })
  },
  // 数组打乱
  shuffle (arr) {
    let i = arr.length;
    while(i) {
      let j = Math.floor(Math.random() * i--);
      [arr[j], arr[i]] = [arr[i], arr[j]];
    }
  },
  // 下一题
  next() {
    let that = this
    if (that.data.curOption == 0) {
      wx.showToast({
        title: '请选择答案',
        icon: 'none',
        duration: 2000
      })
      return false
    } else {
      if (that.data.curOption == that.data.timu[that.data.curIndex].answer) {
        that.data.number++
      } else {
        that.setData({
          allRight: false
        })
      }
      let index = that.data.curIndex
      console.log(index)
      if (index < 8) {
        index++
        that.setData({
          curIndex: index,
          curOption: 0
        })
      } else {
        console.log(that.data.number)
        if (that.data.number == 9) {
          that.setData({
            allRight: true
          })
        }
        that.setData({
          test_end: true
        })
      }
    }
  },
  // 重新玩
  restart () {
    wx.redirectTo({
      url: 'color_index',
    })
  },
  // open 分享和生成图片
  openInvite () {
    let that = this
    that.setData({
      openInvite:true
    })
  },
  closeInvite () {
    let that = this
    that.setData({
      openInvite: false,
      openSave:false
    })
  },
  genePic () {
    let that = this
    that.setData({
      openSave: true
    })
  },
  // 保存图片
  saveImg () {
    let that = this
    if (that.data.isClick) {
      return false
    }
    wx.showLoading({
      title: '保存中',
    })
    wx.getSetting({
      success: function (res) {
        that.setData({
          isClick: true
        })
        wx.authorize({
          scope: 'scope.writePhotosAlbum',
          success: function (resp) {
            let imgUrl = 'https://resource.160nurse.com/canvas/healthy/img/semang/share_img.png'
            wx.downloadFile({
              url: imgUrl,
              success: function (res) {
                wx.hideLoading()
                // console.log(respon);
                wx.saveImageToPhotosAlbum({
                  filePath: res.tempFilePath,
                  success: function (response) {
                    console.log(response)
                    that.setData({
                      isClick: false
                    })
                    wx.hideLoading()
                    wx.showToast({
                      title: '保存成功',
                      icon: 'success'
                    })
                  }
                })
              }
            })
          },
          fail: function () {
            wx.hideLoading()
            that.setData({
              isClick: false
            })

          },
          complete: function (res) {
            // wx.hideLoading()
            console.log(res)
            if (res.errMsg.indexOf('authorize:fail') !== -1) {
              wx.showToast({
                title: '您未打开授权，请前往个人中心授权管理开启授权',
                icon: 'none'
              })
            }
            that.setData({
              isClick: false
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let nickName = '';
    let userId = 0;
    try {
      nickName = wx.getStorageSync('nickname');
      userId = wx.getStorageSync('user_id');
    } catch (e) { }

    return {
      path: "/pages/game/color_index?user_id=" + userId,
      title: '找动物，认字母，一分钟辨别色盲或色弱！快来试试吧！',
      imageUrl: app.resourceDomain + 'canvas/healthy/img/semang/share.png'
    }
  }
})